
/***************************************************************************
 *   Copyright 2010 long2015 liu <long2015@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "rwindow.h"
//#include " h"
#include<QMessageBox>

Rwindow::Rwindow(QWidget *)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
    //初始化界面
    resize(640,480);
    startButton=new QPushButton(tr("开始"),this);
    startButton->setGeometry(500,180,80,40);
    backButton = new QPushButton(tr("悔棋"),this);
    backButton->setGeometry(500,230,80,40);

    connect(backButton,SIGNAL(clicked()),this,SLOT(goBack()));
    //
    chessBoard=QPixmap(480,480);
    drawBoard();

    for(int i=0; i<20; i++)
        for(int j=0; j<20; j++) chess[i][j]=BLANK;
    cols=15;
    bgFile=QString(":/bg/bg3.jpeg");
    nextStep=WHITE;
}

void Rwindow::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
    paint.drawPixmap(0,0,480,480,chessBoard);
    //落子
    QPoint point, p;
    foreach(p, blackList)
    {
        //坐标转换
        point.setX(p.x()*32+OFFSET);
        point.setY(p.y()*32+OFFSET);

        paint.setBrush(QBrush(Qt::black,Qt::SolidPattern));
        paint.drawEllipse(point,15,15);
    }
    foreach(p, whiteList)
    {
        point.setX(p.x()*32+OFFSET);
        point.setY(p.y()*32+OFFSET);

        paint.setBrush(QBrush(Qt::white,Qt::SolidPattern));
        paint.drawEllipse(point,15,15);
    }
}

void Rwindow::drawBoard()
{
    //QPixmap pic=QPixmap(bgFile);
    QPainter pChess(&chessBoard);

      pChess.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
     pChess.drawRect(0,0,480,480);
    //画背景

    //for(int i=0; i<4; i++)
    //    pChess.drawPixmap(0,i*120,480,120,pic);
    //画棋盘线条
    for(int i=0; i<cols; i++)
    {
        pChess.drawLine(OFFSET,OFFSET+(WIDTH+2)*i,448+OFFSET,OFFSET+(WIDTH+2)*i);
        pChess.drawLine(OFFSET+(WIDTH+2)*i,OFFSET,OFFSET+(WIDTH+2)*i,448+OFFSET);
    }
}

void Rwindow::mousePressEvent(QMouseEvent *event)
{
    int x=event->pos().x()-OFFSET, y=event->pos().y()-OFFSET;
    if(x<480-OFFSET)
    {
        QPoint point;
        //四舍五入法算落子坐标
        if( x%32 > 16 ) x=x/32+1;
        else    x=x/32;
        point.setX(x);

        if( y%32 > 16 )  y=y/32+1;
        else    y=y/32;
        point.setY(y);

        if(isPut(point))
        {
            if( nextStep==WHITE)
            {
                whiteList<<point;
                currentPos = point;
                chess[point.y()][point.x()] = WHITE;
                nextStep=BLACK;
            }
            else
            {
                blackList<<point;
                currentPos = point;
                chess[point.y()][point.x()] = BLACK;
                nextStep=WHITE;
            }
            update();
            if( isSucceed()==true)
            {
                QMessageBox::information(this,"ok","you are right.");
            }
        }//if_isPut
    }//if_<480
}

int Rwindow::goBack()
{
    if(nextStep==BLACK)
    {
        if(whiteList.isEmpty())
            return -1;
        chess[currentPos.y()][currentPos.x()] = BLANK;
        nextStep=WHITE;
        currentPos=blackList.last();
        whiteList.removeLast();
    }
    else
    {
        if(blackList.isEmpty())
            return -1;
        chess[currentPos.y()][currentPos.x()] = BLANK;
        nextStep=BLACK;
        currentPos=whiteList.last();
        blackList.removeLast();
    }
    update();
    return 0;
}

bool Rwindow::isSucceed()
{
    int count;
    //横向
    for(int i=0; i<cols; i++)
    {
        count=1;
        for(int j=0; j<cols; j++)
        {
            if(count==5) return true;
            if(chess[i][j]!=BLANK && chess[i][j]==chess[i][j+1])
                count++;
            else
                count=1;
        }
    }
    //竖向
    for(int i=0; i<cols; i++)
    {
        count=1;
        for(int j=0; j<cols; j++)
        {
            if(count==5) return true;
            if(chess[j][i]!=BLANK && chess[j][i]==chess[j+1][i])
                count++;
            else
                count=1;
        }
    }
    //斜向
    return false;
}

bool Rwindow::isPut(QPoint point)
{
    if(chess[point.y()][point.x()]!=BLANK) return false;
    else return true;
}
